<template>
    <view>
        <back />
        <view class="swiperBox" v-if="momentData.imgList">
            <u-swiper
                :list="momentData.imgList"
                keyName="img_path"
                :indicator="true"
                height="400"
            ></u-swiper>
            <view class="upvote" @click="upvoteFn(momentData.id)">
                <image
                    class="icon"
                    :class="momentData.isUpvote && 'animation'"
                    :src="
                        momentData.isUpvote
                            ? '/static/index/like_s.png'
                            : '/static/index/like_k.png'
                    "
                    mode="scaleToFill"
                />
            </view>
        </view>
        <view class="empty" v-else> </view>
        <view class="detailBox">
            <view class="tabsBox">
                <view
                    class="item"
                    :class="current === 0 && 'active'"
                    @click="current = 0"
                    >BASIC</view
                >
                <view
                    class="item"
                    :class="current === 1 && 'active'"
                    @click="current = 1"
                    >REDIENT</view
                >
                <view
                    class="item"
                    :class="current === 2 && 'active'"
                    @click="current = 2"
                >
                    STEPS
                </view>
                <view
                    class="item"
                    :class="current === 3 && 'active'"
                    @click="current = 3"
                    >LABELS</view
                >
            </view>
            <view class="detailContent">
                <view v-if="current === 0">
                    <view class="basicBox">
                        <view class="title"> {{ momentData.title }} </view>
                        <view class="content"> {{ momentData.content }} </view>
                        <view class="upvote">
                            <view class="updateTime">
                                发布时间： {{ momentData.updateTime }}</view
                            >
                            <view class="iconLayout">
                                <image
                                    class="icon"
                                    src="@/static/index/like_s.png"
                                    mode="scaleToFill"
                                />
                                {{
                                    (momentData.upvoteList != undefined &&
                                        momentData.upvoteList.length) ||
                                    0
                                }}
                            </view>
                        </view>
                    </view>
                </view>

                <view class="redientBox" v-if="current === 1">
                    <view class="redientItem"
                        >让我们来看看开始前的准备工作吧~</view
                    >
                    <view
                        class="redientItem"
                        v-for="item in momentData.redient"
                        :key="item.name"
                    >
                        <image
                            class="icon"
                            src="@/static/index/dian.png"
                            mode="scaleToFill"
                        />
                        <view class="name"> {{ item.name }} </view>
                        <view class="weight"> {{ item.weight }} </view>
                    </view>
                </view>
                <view
                    class="redientBox"
                    v-if="current === 2 && stepsList.length > 0"
                >
                    <view class="stepsItem">
                        <view class="count">
                            STEPS {{ stepsList.length }}
                        </view>
                        <view class="txt"> 实现步骤~ </view>
                    </view>
                    <view
                        class="stepsItem"
                        v-for="(item, index) in stepsList"
                        :key="item.id"
                    >
                        <view class="name">
                            {{ `${index + 1}. ${item.content}` }}
                        </view>
                    </view>
                </view>
                <view
                    class="labelsBox"
                    v-if="current === 3 && momentData.labelList"
                >
                    <view
                        class="labelItem"
                        v-for="item in momentData.labelList"
                        :key="item.id"
                    >
                        <image
                            class="icon"
                            src="@/static/index/label.png"
                            mode="scaleToFill"
                        />
                        <view class="name">{{ item.text }}</view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
import dayjs from "dayjs";
import { mapActions, mapState } from "vuex";
import { getMomentByIdApi, upvoteApi } from "@/servers/momentServer.js";
import storage from "@/utils/storage.js";
import { toast } from "@/common/showTips.js";
import back from "@/pages_second/components/back/back.vue";
export default {
    components: { back },
    data() {
        return {
            momentData: {}, // 动态详情
            current: 0, // 当前选择的tab
            upvoteCount: 0, // 点赞数量
        };
    },
    methods: {
        ...mapActions("momentStore", ["getStepsListAction"]),
        async upvoteFn(moment_id) {
            const option = {
                user_id: storage.getCache("userInfo").user_id,
                moment_id,
            };
            const res = await upvoteApi(option);
            const { data } = res;
            if (data) {
                await this.getMomentDetail({ momentId: moment_id });
                toast({ title: "点赞成功~" });
            }
        },
        async getMomentDetail(data) {
            const res = await getMomentByIdApi(data);
            res.data = {
                ...res.data,
                updateTime: dayjs(res.data.updateTime).format("MM-DD HH:mm"),
                labelList: res.data.labelList?.map((labelItem) => {
                    labelItem.text = this.labelList.filter(
                        (l) => l.id === labelItem.label_id
                    )[0].name;
                    return labelItem;
                }),
                isUpvote: !!res.data.upvoteList?.filter(
                    (item) =>
                        item.user_id === storage.getCache("userInfo").user_id
                ).length,
            };
            this.momentData = res.data;
            console.log("this.momentData", this.momentData);
        },
    },
    watch: {
        stepsList(newVal, oldVal) {
            newVal.forEach(
                (item) =>
                    (item.updateTime = dayjs(item.updateAt).format(
                        "MM-DD HH:mm:ss"
                    ))
            );
        },
    },
    async onLoad(options) {
        const { moment_id } = options;
        const data = { momentId: moment_id };
        await this.getMomentDetail(data);
        await this.getStepsListAction(data);
    },
    computed: {
        ...mapState("momentStore", ["stepsList", "labelList"]),
    },
};
</script>
<style>
page {
    background: #f3f5f6;
}
</style>

<style lang="scss" scoped>
.empty {
    width: 100vw;
    height: 50vw;
    background: #fd9c35;
    padding-top: 130rpx;
}
.swiperBox {
    position: relative;
    .upvote {
        width: 96rpx;
        height: 96rpx;
        background: #fff;
        border-radius: 50%;
        position: absolute;
        bottom: 0;
        right: 30rpx;
        transform: translateY(50%);
        display: flex;
        justify-content: center;
        align-items: center;
        .icon {
            width: 48rpx;
            height: 48rpx;
        }
        .animation {
            animation: like-pulse 2.4s 2;
            @keyframes like-pulse {
                0% {
                    transform: scale(1);
                }
                50% {
                    transform: scale(1.3);
                }
                100% {
                    transform: scale(0.8);
                }
            }
        }
    }
}

.detailBox {
    padding: 70rpx 64rpx;

    .basicBox {
        background: #fff;
        border-radius: 36rpx;
        padding: 20rpx 40rpx;
        font-size: 28rpx;
        font-weight: bolder;
        color: #000;
        margin: 36rpx 0;
        box-shadow: 2rpx 2rpx 12rpx #ccc;

        .title {
            font-weight: bolder;
            color: #000;
            font-size: 32rpx;
            padding-bottom: 10rpx;
        }

        .content {
            padding: 10rpx 0;
            line-height: 50rpx;
            font-weight: normal;
        }
        .upvote {
            display: flex;
            align-items: center;
            color: #999;
            font-size: 28rpx;
            font-weight: normal;
            justify-content: space-between;

            .iconLayout {
                display: flex;
                align-items: center;
                .icon {
                    width: 48rpx;
                    height: 48rpx;
                    margin: 20rpx 0;
                }
            }
        }
    }

    .tabsBox {
        display: flex;
        background: #e3e8eb;
        justify-content: space-between;
        align-items: center;
        height: 100rpx;
        border-radius: 60rpx;
        font-size: 24rpx;
        font-weight: 600;
        color: #666;
        padding: 0 10rpx;

        .active {
            background: #fff;
            color: #000;
            animation: myfirst 0.5s;
            @keyframes myfirst {
                0% {
                    margin: -10rpx -10rpx;
                }
                50% {
                    margin: 10rpx 10rpx;
                }
                100% {
                    margin: 0rpx;
                }
            }
        }
        .item {
            width: 30%;
            height: 80rpx;
            line-height: 80rpx;
            text-align: center;
            border-radius: 32rpx;
        }
    }

    .detailContent {
        .redientBox {
            margin-top: 60rpx;
            color: #5e5e5e;
            font-size: 28rpx;
            font-weight: 600;
            padding-bottom: 60rpx;

            .redientItem {
                display: flex;
                align-items: center;
                background: #fff;
                height: 80rpx;
                line-height: 80rpx;
                padding: 0 40rpx;
                border-bottom: #f4f6f7 solid 2rpx;

                &:nth-child(1) {
                    height: 88rpx;
                    line-height: 88rpx;
                    text-align: center;
                    border-radius: 32rpx;
                }

                &:nth-child(2) {
                    margin-top: 40rpx;
                    border-radius: 32rpx 32rpx 0 0;
                }
                &:last-child {
                    border-radius: 0 0 32rpx 32rpx;
                    border-bottom: 0;
                }

                .weight {
                    margin: 0 32rpx;
                    color: #000;
                }
                .name {
                    margin: 0 32rpx;
                    width: 120rpx;
                }

                .icon {
                    width: 12rpx;
                    height: 12rpx;
                }
            }

            .stepsItem {
                color: #5e5e5e;
                background: #fff;
                padding: 24rpx 40rpx;
                border-bottom: #f4f6f7 solid 2rpx;

                .icon {
                    width: 12rpx;
                    height: 12rpx;
                }

                &:nth-child(1) {
                    background: #7a7ac7;
                    color: #fff;
                    font-size: 32rpx;
                    font-weight: 600;
                    border-radius: 48rpx;
                    border: 0;
                    display: block;
                    padding: 0;
                    .count {
                        width: 100%;
                        height: 88rpx;
                        line-height: 88rpx;
                        text-align: center;
                        background: #fff;
                        color: #7a7ac7;
                        border-radius: 48rpx 48rpx 0 0;
                    }
                    .txt {
                        line-height: 88rpx;
                        text-align: center;
                        font-size: 28rpx;
                    }
                }
                &:nth-child(2) {
                    margin-top: 40rpx;
                    border-radius: 32rpx 32rpx 0 0;
                }
                &:last-child {
                    border-radius: 0 0 32rpx 32rpx;
                    border-bottom: 0;
                }
            }
        }
        .labelsBox {
            padding-top: 40rpx;
            display: flex;
            flex-wrap: wrap;
            justify-content: start;
            align-items: center;

            .labelItem {
                background: #fff;
                height: 88rpx;
                color: #000;
                font-size: 28rpx;
                font-weight: bolder;
                border-radius: 28rpx;
                margin-bottom: 36rpx;
                display: flex;
                justify-content: center;
                align-items: center;
                padding: 0 30rpx;
                margin-right: 20rpx;
                border: #e3e8eb 2rpx solid;
                // background: #7a7ac7;
                // height: 88rpx;
                // color: #fff;
                // font-size: 28rpx;
                // font-weight: bolder;
                // border-radius: 28rpx;
                // margin-bottom: 36rpx;
                // display: flex;
                // justify-content: center;
                // align-items: center;
                // padding: 0 30rpx;
                // margin-right: 20rpx;
                .icon {
                    width: 48rpx;
                    height: 48rpx;
                    margin-right: 20rpx;
                }
            }
        }
    }
}
</style>
